%Continuation to question II_2
clc;
for i=1:2
    if i==2
        ds1_sub = ds1;
        ds2_sub = ds2;
    end
    ds1_sub_aug = [ds1_sub ones(size(ds1_sub,1),1)];
    ds2_sub_aug = [ds2_sub ones(size(ds2_sub,1),1)];
    
    % option 1
    K1 = cov(ds1_sub_aug)+mean(ds1_sub_aug)'*mean(ds1_sub_aug);
    K2 = cov(ds2_sub_aug)+mean(ds2_sub_aug)'*mean(ds2_sub_aug);
    w = (w1.P*K1+w2.P*K2)^-1*(w1.P*mean(ds1_sub_aug)'-w2.P*mean(ds2_sub_aug)');
    % compute error
    new1_aug = [new1  ones(size(new1,1),1)];
    new2_aug = [new2  ones(size(new2,1),1)];
    
    ne1 = sum(new1_aug*w <= 0); % data that classified to class 2
    ne2 = sum(new2_aug*w > 0);
    E_opt1 = w1.P*ne1/size(new1_aug,1)+w2.P*ne2/size(new2_aug,1);
    disp(['for option 1, E_lmse=',num2str(E_opt1)]);
    
    % option 2
    M = (w1.P*mean(ds1_sub)+w2.P*mean(ds2_sub))';
    S = w1.P*cov(ds1_sub)+w2.P*cov(ds2_sub);
    delt = (mean(ds1_sub)-mean(ds2_sub))*(S^-1)*(mean(ds1_sub)-mean(ds2_sub))';
    V_d = -M'*(S^-1)*(mean(ds1_sub)'-mean(ds2_sub)')+(w1.P-w2.P)/(2*w1.P*w2.P)*(1+delt*w1.P*w2.P);
    v = (S^-1)*(mean(ds1_sub)-mean(ds2_sub))';
    ne1 = sum(new1*v + V_d <= 0); % data that classified to class 2
    ne2 = sum(new2*v + V_d > 0);
    E_opt2 = w1.P*ne1/size(new1,1)+w2.P*ne2/size(new2,1);
    disp(['for option 2, E_lmse=',num2str(E_opt2)]);
    
    % option 3
    v3 = (S^-1)*(mean(ds1_sub)-mean(ds2_sub))';
    V_d = -100:0.1:100;
    E_log = [];
    for v_d3 = V_d
        ne1 = sum(test1*v3 + v_d3 <= 0); % data that classified to class 2
        ne2 = sum(test2*v3 + v_d3 > 0);
        E = w1.P*ne1/size(test1,1)+w2.P*ne2/size(test2,1);
        E_log = [E_log E];
    end
    [E,idx] = min(E_log);
    disp(['E option 3 for test is: ',num2str(E)])
    v_d3 = V_d(idx);
    % using the chosen V_d+1 calc the real error
    ne1 = sum(new1*v3 + v_d3 <= 0); % data that classified to class 2
    ne2 = sum(new2*v3 + v_d3 > 0);
    E_opt3 = w1.P*ne1/size(new1,1)+w2.P*ne2/size(new2,1);
    disp(['for option 3, E_lmse=',num2str(E_opt3)]);
end